//
//  CS_Pop_Progress.m
//  Pods
//
//  Created by 网新中研 on 2017/6/30.
//
//

#import "CS_Pop_Progress.h"
#import <math.h>
#import <UIKit/UIKit.h>

@interface CS_Pop_Progress()

@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UIView *view2;
@property (nonatomic, strong) UIView *view3;
@property (nonatomic, strong) UIView *view4;
@property (nonatomic, strong) UIView *view5;
@property (nonatomic, strong) UIView *view6;

@end

#define kLineLength 20.0
#define kLineWidth 2.0
#define kWidth 160.0
#define kLeadMargin (kWidth - kLineLength)/2.0
#define kLineMargin kLineLength/4.0

@implementation CS_Pop_Progress

-(instancetype)init{
    if (self = [super init]) {
        [self layoutView];
    }
    return self;
}

-(void)layoutView{
    
    UIImage *circleImage = [self generateCircleImage];
    UIImageView *circleImageView = [[UIImageView alloc]initWithImage:circleImage];
    circleImageView.frame = CGRectMake(0, 0, circleImage.size.width, circleImage.size.height);
    
    [self addSubview:circleImageView];
    
    self.frame = circleImageView.frame;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue = [NSNumber numberWithFloat:M_PI*(2.0)];
    animation.duration = 0.65;
    animation.autoreverses = NO;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    
    animation.repeatCount = MAXFLOAT;
    
    [self.layer addAnimation:animation forKey:nil];
    
    return;
    
    
    
    UIImage *cloudImage = [self generateCloudImage];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:cloudImage];
    
    imageView.frame = CGRectMake(0, 0, cloudImage.size.width, cloudImage.size.height);

    [self addSubview:imageView];

    
    UIImage *cloudImage1 = [self generateCloudImage];
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithImage:cloudImage1];
    
    imageView1.frame = CGRectMake(60, 110, cloudImage1.size.width, cloudImage1.size.height);
    
    [self addSubview:imageView1];
    
    self.frame = CGRectMake(0, 0, kWidth, kWidth);
    
}

/**
 * 显示云彩飘动动画
 */
-(void)showCloudFlowAnimation{
    
    UIImageView *cloudImageView = [self generateCloudImageView];
    cloudImageView.layer.cornerRadius = kWidth/2.0;
    cloudImageView.layer.shadowOpacity = 0.3;
    cloudImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    
}

/**
 * 随机生成云彩视图
 */
-(UIImageView *)generateCloudImageView{
    
    UIImage *cloudImage = [self generateCloudImage];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:cloudImage];
    
    imageView.frame = CGRectMake(0, 0, cloudImage.size.width, cloudImage.size.height);
    
    return imageView;
    
}

-(void)popShow{
    
    KLCPopupLayout layout = KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter,
                                               KLCPopupVerticalLayoutCenter);
    
    KLCPopup* popup = [KLCPopup popupWithContentView:self
                                            showType:KLCPopupShowTypeGrowIn
                                         dismissType:KLCPopupDismissTypeShrinkOut
                                            maskType:KLCPopupMaskTypeClear
                            dismissOnBackgroundTouch:YES
                               dismissOnContentTouch:NO];
    self.superKLCPopup = popup;
    [popup showWithLayout:layout];
    
    
    CFTimeInterval pausedTime = [self.layer timeOffset];
    self.layer.speed = 1.0;
    self.layer.timeOffset = 0.0;
    self.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.layer.beginTime = timeSincePause;
    
}



- (void)addBounceToViewX:(UIView *)view delaySecond:(CGFloat)delaySeconds{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CABasicAnimation *floatSide = [CABasicAnimation animationWithKeyPath:@"position.x"];
        floatSide.fromValue = [NSNumber numberWithFloat:view.center.x - 5];
        floatSide.toValue = [NSNumber numberWithFloat:view.center.x + 5];
        floatSide.duration = 0.3f;
        floatSide.autoreverses = YES;
        floatSide.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        floatSide.repeatCount = HUGE_VALF;
        
        
        [view.layer addAnimation:floatSide forKey:nil];
    });
    
}

- (void)addBounceToViewY:(UIView *)view delaySecond:(CGFloat)delaySeconds{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        CABasicAnimation *floatDown = [CABasicAnimation animationWithKeyPath:@"position.y"];
        floatDown.fromValue = [NSNumber numberWithFloat:view.center.y - 5];
        floatDown.toValue = [NSNumber numberWithFloat:view.center.y + 5];
        floatDown.duration = 0.3f;
        floatDown.autoreverses = YES;
        floatDown.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        floatDown.repeatCount = HUGE_VALF;

        [view.layer addAnimation:floatDown forKey:nil];
        
    });
    
    
    
}



-(void)doAnimation{
    
}



#pragma mark --- 绘制云彩动画

/**
 * 获取随机值
 */
-(int)getRandomInt:(int)lowerInt uperInt:(int)uperInt{
    
    UInt32 range = (UInt32)(uperInt- lowerInt);
    
    return lowerInt + (int)(arc4random_uniform(range));
    
}

/**
 * 生成贝塞尔圆
 */
-(UIBezierPath *)getCircle:(CGPoint)center radius:(CGFloat)radius{
    return [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:(CGFloat)(2 * M_PI) clockwise:YES];
}

-(void)drawCloud{
    
    //随机生成拖椭圆的长轴
    CGFloat a = (CGFloat)[self getRandomInt:30 uperInt:70];
    
    //所及生成椭圆的短轴
    CGFloat b = (CGFloat)[self getRandomInt:10 uperInt:23];
    
    //在椭圆上生成点
    CGPoint points[12];
    for (int i = 0; i < 12; i++) {
        
        CGPoint point = CGPointMake(a * cos(2 * M_PI * i / 12.0), b * sin(2 * M_PI * i / 12.0));
        
        points[i] = point;
        
    }
    
}

-(UIImage *)generateCircleImage{

    //圆圈上点的个数
    int pointNumber = 5;
    
    CGFloat circleRidius = 13.0;
    
    CGPoint points[pointNumber];
    for (int i = 0; i < pointNumber; i++) {
        
        CGPoint point = CGPointMake(circleRidius * cos(M_PI*1.5 / pointNumber * i + M_PI / 2.0), circleRidius * sin(M_PI*1.5 / pointNumber * i + M_PI / 2.0));
        
        points[i] = point;
        
    }
    
    UIBezierPath *bezierPath = [UIBezierPath new];

    for (int i = 0; i < pointNumber; i++) {
        CGFloat radius = 1 + (i / 2) * 1;
        CGPoint point = points[i];
        UIBezierPath *circleBezierPath = [self getCircle:point radius:radius];
        [bezierPath appendPath:circleBezierPath];
    }
    
    CGFloat length = bezierPath.bounds.size.width > bezierPath.bounds.size.height ? bezierPath.bounds.size.width : bezierPath.bounds.size.height;
    
    CGSize circleSize = CGSizeMake(length, length);
    
    CGFloat margin = 20.0;
    
    UIGraphicsBeginImageContext(CGSizeMake(circleSize.width + margin, circleSize.height + margin));
    
    [COLOR_THEME setFill];
    
    [bezierPath applyTransform:CGAffineTransformMakeTranslation(circleSize.width / 2.0 + margin/2.0, circleSize.height / 2.0 + margin/2.0)];
    
    [bezierPath fill];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    return image;
    
}

-(UIImage *)generateCloudImage{
    
    int ndiv = 7;
    
    //随机生成拖椭圆的长轴
    CGFloat a = (CGFloat)[self getRandomInt:20 uperInt:40];
    
    //所及生成椭圆的短轴
    CGFloat b = (CGFloat)[self getRandomInt:5 uperInt:10];
    
    //在椭圆上生成点
    CGPoint points[ndiv];
    for (int i = 0; i < ndiv; i++) {
        
        CGPoint point = CGPointMake(a * cos(2 * M_PI * i / ndiv), b * sin(2 * M_PI * i / ndiv));
        
        points[i] = point;
        
    }
    
    //绘制椭圆闭环
    UIBezierPath *bezierPath = [UIBezierPath new];
    [bezierPath moveToPoint:points[0]];
    
    for (int i = 1; i < ndiv; i++) {
        [bezierPath addLineToPoint:points[i]];
    }
    
    [bezierPath closePath];
    
    //绘制椭圆坐标上的点
    int minRadius = (int)(M_PI * a / ndiv);
    int maxRadius = minRadius + 10;
    
    for (int i = 0; i < ndiv; i++) {
        int randomRadius = [self getRandomInt:minRadius uperInt:maxRadius];
        CGPoint point = points[i];
        UIBezierPath *circleBezierPath = [self getCircle:point radius:randomRadius];
        [bezierPath appendPath:circleBezierPath];
    }
    
    CGSize cloudSize = CGSizeMake(bezierPath.bounds.size.width, bezierPath.bounds.size.height);
    CGFloat margin = 20.0;
    
    UIGraphicsBeginImageContext(CGSizeMake(cloudSize.width + margin, cloudSize.height + margin));
    [COLOR_WHITE setFill];
    
    [bezierPath applyTransform:CGAffineTransformMakeTranslation(cloudSize.width / 2.0 + margin / 2.0, cloudSize.height / 2.0 + margin / 2.0)];
    [bezierPath fill];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    return image;
}


@end

@interface CSCloudView : UIImage

@end

@implementation CSCloudView




@end
